﻿using Microsoft.AspNetCore.Mvc.Filters;
using System.Text.Json;

namespace My.WebApi.Filters
{
    public class GlobalExceptionFilter : Volo.Abp.AspNetCore.Mvc.ExceptionHandling.AbpExceptionFilter
    {
        protected override Task HandleAndWrapException(ExceptionContext context)
        {
            var errorObject = new
            {
                Message = context.Exception.Message,
                // 其他错误相关信息
            };

            context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            context.HttpContext.Response.ContentType = "application/json";
            context.HttpContext.Response.WriteAsync(JsonSerializer.Serialize(errorObject));
            return Task.CompletedTask;
        }

        protected override bool ShouldHandleException(ExceptionContext context)
        {
            return true;
        }
    }
}
